// pages/order/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail:{
      storeImg:'../../../libs/images/logo.png',
      orderState:1,
      orderStateTxt:'待接单',
      stateExplain:'请耐心等待商家正在处理',
      storeName:'海底捞（蛇口店）',
      dishList:[
        {dishName:'麻辣香锅',dishNum:1,dishPrice:25},
        { dishName: '米饭', dishNum: 2, dishPrice: 3 },
        { dishName: '水煮白菜', dishNum: 1, dishPrice: 5 }
      ],
      otherList:[
        {otherName:'配送费',otherPrice:3},
        { otherName: '餐盒费', otherPrice: 1 }
      ],
      activeList:[
        { activeName: '满30减2元',activePrice:2}
      ],
      couponName:'优惠券',
      couponPrice:'',
      totalPrice:33,
      orderAddrIcon:3,
      orderAddr:'广东省深圳市南山区科技大厦 科技大厦二期A栋202',
      userName:'杨光（先生）',
      phoneNum:13566232323,
      arriveTime:'尽快送达',
      orderMsg:{
        orderNumber:20180403114502,
        orderTime:'2018.04.03 17:2s0:35',
        orderPayWay:'微信支付',
        orderRemark:'多放辣椒少放盐，配菜多一点'
      },
      chargebackTxt: '骑手已开始配送',
      contactNum:'15361573664'

    },
    clock: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    var that = this;
    if (this.data.orderDetail.orderState == 1){
      that.count_down();  
    }else{

    }
    
  },
  /* 毫秒级倒计时 */
  count_down: function () {
    var that = this
    //2016-12-27 12:47:08 转换日期格式  即下单时间
    var a = that.data.orderDetail.orderMsg.orderTime.split(/[^0-9]/);
    //截止日期：日期转毫秒  
    var expireMs = new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
    //倒计时毫秒  
    var duringMs = expireMs.getTime() + 30 * 60 * 1000 - (new Date()).getTime();
    // 渲染倒计时时钟  
    that.setData({
      clock: that.date_format(duringMs)
    });

    if (duringMs <= 0) {
      that.setData({
        clock: "已超时"
      });
      // timeout则跳出递归  
      return;
    }
    setTimeout(function () {
      // 放在最后--  
      duringMs -= 10;
      that.count_down();
    }
      , 10)
  },
  /* 格式化倒计时 */
  date_format: function (micro_second) {
    var that = this
    // 秒数  
    var second = Math.floor(micro_second / 1000);
    // 小时位  
    var hr = Math.floor(second / 3600);
    // 分钟位  
    var min = that.fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
    // 秒位  
    var sec = that.fill_zero_prefix(second % 60);// equal to => var sec = second % 60;
    return min + ":" + sec + " ";
  },

  /* 分秒位数补0 */
  fill_zero_prefix: function (num) {
    return num < 10 ? "0" + num : num
  },
  timestampToTime: function (timestamp){
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    Y = date.getFullYear() + '-';
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() + ':';
    s = date.getSeconds();
    return Y + M + D + h + m + s;
  },
  
  chargeCall:function(){
    wx.showToast({
      title: '下单30分钟，才可以催单喔~',
      icon: 'none',
      duration: 2000
    })
  },
  chargeback: function () {
    var that = this;
    wx.showModal({
      title: '商家已接单，是否确认申请退单',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showModal({
            title: '商家拒绝退单',
            content: '理由：' + that.data.orderDetail.chargebackTxt,
            success: function (res) {
              if (res.confirm) {

              } else if (res.cancel) {

              }
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  // 再来一单
  orderAgain: function (e) {

  },
  // 前往支付
  gotoPay: function () {

  },
  // 点击优惠券
  toCoupon:function(){

  },
  // 联系商家
  contactStore:function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.orderDetail.contactNum
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})